package com.micro.service.gateway.web.rest.user;

import com.micro.service.account.share.dto.UserBaseDto;
import com.micro.service.account.share.service.UserBaseService;
import com.micro.service.common.dto.ResultDTO;
import com.micro.service.gateway.vo.UserTokenInfo;
import com.micro.service.gateway.vo.UserVo;
import com.micro.service.gateway.web.rest.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lvxiucai
 * @description 用户服务
 * @date 2020/4/13
 */
@RestController
@Slf4j
@RequestMapping("/user/v1")
public class UserController extends BaseController{

    @Autowired
    private UserBaseService userBaseService;

    @RequestMapping("/getUserInfo")
    public ResultDTO<UserVo> getUserInfo(){
        ResultDTO<UserVo> result = new ResultDTO<>();

        UserTokenInfo userInfoFromToken = getUserInfoFromToken();
        if(userInfoFromToken == null){
            log.error("用户未登录或令牌已失效，userInfoFromToken:{}",userInfoFromToken);
            return result.failure("用户未登录或令牌已失效");
        }
        if(userInfoFromToken.getUserId() != null){
            UserBaseDto userBaseDto = userBaseService.selectById(userInfoFromToken.getUserId());
            UserVo vo = new UserVo();
            BeanUtils.copyProperties(userBaseDto,vo);
            return result.success(vo);
        }

        return result.failure("未查询到用户信息，请稍后重试！");
    }

}
